import { Component, Layout, Sprite, _decorator } from "cc";
import { DEV } from "cc/env";
const { ccclass, property, requireComponent } = _decorator;
@ccclass('SuperLayout')
export class SuperLayout extends Component {
    @property({ tooltip: DEV && "是否开启自动布局" })
    @property
    autoLayout: boolean = true;

    start(): void {
        this.initLayout();
    }

    private initLayout() {
        if (!this.autoLayout) {
            return;
        }
        let sprite = this.node.getComponent(Sprite);
        if (!sprite) {
            return;
        }
        let layout = this.node.getComponent(Layout);
        if (!layout) {
            layout = this.node.addComponent(Layout);
            // layout.alignHorizontal = false;
            layout.type = Layout.Type.HORIZONTAL;
            layout.resizeMode = Layout.ResizeMode.CONTAINER;
            layout.spacingX = 6;
            // layout.horizontalDirection = Layout.HorizontalDirection.LEFT_TO_RIGHT;
            // layout.affectedByScale = false;
        }
        if (sprite.spriteFrame) {
            layout.paddingLeft = sprite.spriteFrame.insetLeft;
            layout.paddingRight = sprite.spriteFrame.insetRight;
        }
        sprite.type = Sprite.Type.SLICED;
        layout.updateLayout(true);
    }
}